package com.zhugy.comparecode.util;

import java.util.Collection;

import com.zhugy.comparecode.CompareResult;

public class CommonUtils {

	public static String trim(String line) {
		if (line == null || line.length() == 0) {
			return line;
		}
		int before = 0, after = 0;
		for (int i = 0; i < line.length(); i++) {
			if (!Character.isWhitespace(line.charAt(i))) {
				before = i;
				break;
			}
		}
		for (int i = line.length(); i > 0; i--) {
			if (!Character.isWhitespace(line.charAt(i - 1))) {
				after = i;
				break;
			}
		}
		if (before <= after) {
			return line.substring(before, after);
		}
		return line;
	}
	
	public static CompareResult compareContent(String c1, String c2) {
		int i = 0, j = 0;
		while (i < c1.length()) {
			while (Character.isWhitespace(c1.charAt(i))) {
				i++;
				if (i == c1.length()) {
					break;
				}
				continue;
			}
			if (i == c1.length()) {
				break;
			}
			char ch1 = c1.charAt(i);
			char ch2 = 0;
			while (j < c2.length()) {
				ch2 = c2.charAt(j);
				while (Character.isWhitespace(ch2)) {
					j++;
					ch2 = c2.charAt(j);
				}
				break;
			}

			if (j >= c2.length()) {
				return CompareResult.fail("有效代码长度不一样：【" + c1 + "】长度大于【" + c2 + "】");
			}
			if (ch1 != ch2) {
				return CompareResult
						.fail("有效代码长度不一样：【" + c1 + "】的第" + (i + 1) + "字符不等于【" + c2 + "】的" + (j + 1) + "字符");
			}
			i++;
			j++;
		}
		if (j < c2.length()) {
			boolean isAllBlank = true;
			while (j < c2.length()) {
				if (!Character.isWhitespace(c2.charAt(j))) {
					isAllBlank = false;
					break;
				}
				j++;
			}
			if (!isAllBlank) {
				return CompareResult.fail("有效代码长度不一样：【" + c1 + "】长度小于【" + c2 + "】");
			}
		}
		return CompareResult.success("");
	}
	
	public static boolean isEmpty(String s) {
		return s == null || s.length() == 0;
	}
	
	public static String replace(String s, String s1, String s2) {
		if (isEmpty(s)) {
			return s;
		}
		return s.replace(s1, s2);
	}

	public static String concatPath(String path1, String path2) {
		if (isEmpty(path1) && isEmpty(path2)) {
			throw new RuntimeException("路径不能为空");
		}
		String p1 = replace(path1, "\\", "/");
		String p2 = replace(path2, "\\", "/");
		if (isEmpty(p1)) {
			return p2;
		}
		if (isEmpty(p2)) {
			return p1;
		}
		while (p1.endsWith("/")) {
			p1 = p1.substring(0, p1.length() - 1);
		}
		while (p2.startsWith("/")) {
			p2 = p2.substring(1, p2.length());
		}
		return p1 + "/" + p2;
	}
	
	public static String extractSimpleName(String fullPath) {
		if (isEmpty(fullPath)) {
			throw new RuntimeException("路径不能为空");
		}
		String p1 = replace(fullPath, "\\", "/");
		int lastIndexOfSlash = p1.lastIndexOf("/");
		if (lastIndexOfSlash < 0) {
			return p1;
		}
		return p1.substring(lastIndexOfSlash + 1);
	}
	
	public static String extractDirectory(String fullPath) {
		if (isEmpty(fullPath)) {
			throw new RuntimeException("路径不能为空");
		}
		String p1 = replace(fullPath, "\\", "/");
		int lastIndexOfSlash = p1.lastIndexOf("/");
		if (lastIndexOfSlash < 0) {
			return p1;
		}
		return p1.substring(0, lastIndexOfSlash);
	}
	
	public static int size(Collection<?> coll) {
		return coll == null ? 0 : coll.size();
	}
	
	public static boolean isEmpty(Collection<?> coll) {
		return coll == null || coll.isEmpty();
	}
	
	public static boolean equals(String s1, String s2) {
		if (s1 == null) {
			if (s2 == null) {
				return true;
			}
			return false;
		}
		if (s2 == null) {
			return false;
		}
		return s1.equals(s2);
	}
}
